<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" id="minwidth" >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
RM = {};
RM.Pages = {};
RM.Pages.User = {};
RM.Pages.User.Unit = {};

RM.Common = {};
RM.Common.GUIDateFormat = "<?php echo $this->guiDateFormat ?>";
RM.Common.MySQLDateFormat = "<?php echo $this->mysqlDateFormat ?>";
RM.Common.PHPDateFormat = "<?php echo $this->phpDateFormat ?>";
RM.Common.CalendarStartDay = <?php echo $this->calStartDay ?>;

RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';
RM.BaseLargeImageURL = RM.RootURL + 'RM/userdata/images/system/large/';
RM.BaseSmallImageURL = RM.RootURL + 'RM/userdata/images/system/small/';
RM.BaseMenuImageURL  = RM.RootURL + 'RM/userdata/images/system/menu/';
RM.BaseUnitImageURL  = RM.RootURL + 'RM/userdata/images/media/';

<?php echo $this->_router->getAccembleURLJsCode(); ?>
</script>

<?php
// set if the js files are loaded from debug or live(compressed)...
if ((int)Zend_Registry::get('config')->get('js')->get('debug')->get('user') == 1) {
    $jsLoadFolder = '/debug/';
} else {
    $jsLoadFolder = '/';
}
?>

<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js<?php echo $jsLoadFolder;?>admin/common.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js<?php echo $jsLoadFolder;?>admin/lists.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getInstance()->getRouter()->_('Language', 'getconstants'); ?>"></script>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- ExtJS Base -->
<?php
$config = Zend_Registry::get('config');
if ((int)$config->get('js')->get('debug')->get('user') == 1) {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
<?php
} else {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script>
<?php
}
?>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- calendar -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/datepickerplus/datepickerplus.css"/>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/datepickerplus/ext.ux.datepickerplus.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/rm/Ext.rm.form.DateField.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/rm/Ext.rm.Calendar.js"></script>
<?php
if ((int)Zend_Registry::get('config')->get('js')->get('debug')->get('user') == 1) {?>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user-debug.css"/>
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user.css"/>
<?php } ?>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/DayRent/css/datepicker.css"/>
<?php
$config = new RM_Config;
$dateFormat = $config->getJSDateformat();
$CalStartDay = $config->getValue('rm_config_calendar_startday');
$disabledPeriods = $this->calendardata;

// get the unit price config
$unitConfig = new RM_UnitDayRentConfig();
$enable_timeselection = (int)$unitConfig->fetchValueByUnit($this->unit_id, 'enable_timeselection');

$timeselection_increment = (int)$unitConfig->fetchValueByUnit($this->unit_id, 'timeselection_increment');
$timeselection_increment = ($timeselection_increment !== 0 ? $timeselection_increment : 15); // set this to default if the value is null

$rental_end_time = $unitConfig->fetchValueByUnit($this->unit_id, 'rental_end_time');
$rental_end_time = ($rental_end_time !== "" ? $rental_end_time : "09:00"); // default of 9:00 if null

$rental_start_time = $unitConfig->fetchValueByUnit($this->unit_id, 'rental_start_time');
$rental_start_time = ($rental_start_time !== "" ? $rental_start_time : "17:00"); // default of 17;00 if null

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

if ($dateFormat=="d/m/Y"){
    $lng_dateFormat = "dd/mm/yyyy";
} else {
    $lng_dateFormat = "mm/dd/yyyy";
}

$qtyCriteria  = 1; // the number of units selected (there will always be 1)
$qtyAvailable = 1; // the number of units available (used for the dropdown list)

if (isset($_GET['isGroup']) && (int)$_GET['qty']>1 ){
    $qtyAvailable = (int)$_GET['qty'];
}

if (isset($criteria->quantity) && $criteria->quantity>1){
    $qtyCriteria = $criteria->quantity;
}
?>
<div class="RM_datepicker_modal_container">
    <div class="RM_datepicker_modal_header">
            <?php echo $this->_translate->_('User.DatePicker','DateSelection');?>
    </div>
    <div class="RM_dateselector">
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('StartDate');?></div></td>
        <td width="10" nowrap><div id="RM_start_dp_calendar"></div></td>
        <td width="100" nowrap><div id="RM_start_time_selection"></div></td>
      </tr>
      <tr>
        <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('EndDate');?></div></td>
        <td width="10" nowrap><div id="RM_end_dp_calendar"></div></td>
        <td width="100" nowrap><div id="RM_end_time_selection"></div></td>
      </tr>
    </table>
    <div id="RM_quantity_selection" style="display: none;">
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td><div class="RM_quanitity_selection_label"><?php echo $this->_translate->_('QuantityToReserve');?></div></td>
        <td>
            <div id="RM_quanitity_selection">
            <select id="RM_dateselector_qty" name="RM_dateselector_qty">
            <?php
            if (isset($_GET['isGroup']) && $qtyAvailable>1 ){
                for ($i = 1; $i <= $qtyAvailable; $i++) {
                    if ($qtyCriteria == $i){
                        ?><option selected value="<?php echo $i ?>" ><?php echo $i; ?></option><?php
                    } else {
                        ?><option value="<?php echo $i ?>" ><?php echo $i; ?></option><?php
                    }
                }
            }?>
            </select>
            </div>
        </td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </div>
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td colspan="3">
            <div id="RM_dateselector_message">
                <?php echo $this->_translate->_('User.DatePicker','HelpMsg');?>
            </div>
        </td>
      </tr>
    </table>
    <div id="RM_dateselector_button">
        <input name="" type="button" value="OK" class="button" onclick="RM_dp_Submit()" />
    </div>
    </div>
</div>

<script type="text/javascript">

var RM_dp_viewFormat = '<?php echo $dateFormat; ?>';
var RM_dp_viewRenderFormat = '<?php echo $lng_dateFormat; ?>';
var RM_dp_submitFormat = 'Y-m-d';

function RM_dp_Submit(){

    document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','SavingDates');?>";
    document.getElementById('RM_dateselector_button').style.display = "none";
    var quantity = document.getElementById('RM_dateselector_qty').value;

    var startDate = Date.parseDate(Ext.getCmp('rm_date_startdate').value + " " + Ext.getCmp('rm_time_starttime').value, RM_dp_viewFormat + " H:i");
    var endDate = Date.parseDate(Ext.getCmp('rm_date_enddate').value + " " + Ext.getCmp('rm_time_endtime').value, RM_dp_viewFormat + " H:i");

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Reservations',
            action: 'setdatecriteriajson'
        }),
        params: {
            unit_id : <?php echo $this->unit_id;?>,
            startdate: startDate.format(RM.Common.MySQLDateFormat),
            enddate: endDate.format(RM.Common.MySQLDateFormat),
            adults: 1,
            children: 0,
            infants: 0,
            qty: quantity
        },
        method: 'GET',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json.success===true){
                // if the config is set to go to cart on selection
                if ("1" === "<?php echo $config->getValue('rm_config_gotocartsfterselection_enabled'); ?>"){
                    // cart url
                    var cartURL = RM.Common.AssembleURL({
                        controller : 'Reservations',
                        action: 'cart'
                    });
                    window.parent.location = cartURL;
                } else {
                    // reload the parent window
                    window.parent.location.reload();
                }
            } else {
                document.getElementById('RM_dateselector_message').innerHTML = json.error;
                document.getElementById('RM_dateselector_button').style.display = "block";
            }
        },
        failure: function(){
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};

Ext.onReady(function(){

    var disabledData = <?php echo $disabledPeriods; ?>;       

    <?php
    if (is_object($criteria) && $criteria->start_datetime) {
        ?>
        var startDate = RM.Common.ConvertToDate('<?php echo date("Y-m-d", strtotime($criteria->start_datetime));?>');
        <?php
    } else {
        ?>
        var startDate = new Date();
        <?php
    }
    if (is_object($criteria) && $criteria->end_datetime) {
        ?>
        var endDate = RM.Common.ConvertToDate('<?php echo date("Y-m-d", strtotime($criteria->end_datetime));?>');
        <?php
    } else {
        ?>
        var endDate = new Date();
        <?php
    }
    ?>


    var startDatePicker = new Ext.rm.form.DateField({
            id: "rm_date_startdate",
            startDay: <?php echo $CalStartDay;?>,
            disabledDatePeriods: disabledData.start,
            allowBlank: false,
            readOnly: false,
            showActiveDate: true,
            disabledLetter: "X",
            value: startDate,
            minDate: new Date(),
            useQuickTips: false,
            markNationalHolidays: false,
            showWeekNumber: false,
            width: 150,
            format: "<?php echo $dateFormat;?>",
            listeners: {
                select: function(dp,date) {
                    Ext.getCmp('rm_date_enddate').setValue(date);
                }
            }
    });

    startDatePicker.render('RM_start_dp_calendar');

    var endDatePicker = new Ext.rm.form.DateField({
            id: "rm_date_enddate",
            startDay: <?php echo $CalStartDay;?>,
            disabledDatePeriods: disabledData.end,
            allowBlank: false,
            readOnly: false,
            showActiveDate: true,
            disabledLetter: "X",
            value: endDate,
            minDate: new Date(),
            useQuickTips: false,
            markNationalHolidays: false,
            showWeekNumber: false,
            width: 150,
            format: "<?php echo $dateFormat;?>"
    });

    endDatePicker.render('RM_end_dp_calendar');

    <?php
    if ($enable_timeselection == 1){
    ?>

    // time selection fields

    var startTimeSelector = new Ext.form.TimeField({
        id: "rm_time_starttime",
        minValue: '<?php echo $rental_start_time;?>',
        maxValue: '<?php echo $rental_end_time;?>',
        width: 100,
        increment: <?php echo $timeselection_increment?>,
        format: "H:i",
        value: '<?php echo $rental_start_time;?>'
    });

    startTimeSelector.render('RM_start_time_selection');

    var endTimeSelector = new Ext.form.TimeField({
        id: "rm_time_endtime",
        minValue: '<?php echo $rental_start_time;?>',
        maxValue: '<?php echo $rental_end_time;?>',
        width: 100,
        increment: <?php echo $timeselection_increment?>,
        format: "H:i",
        value: '<?php echo $rental_end_time;?>'
    });

    endTimeSelector.render('RM_end_time_selection');

    <?php } else { ?>

    // hidden fields for start/end date, used if the times are disabled

    var startTimeSelector = new Ext.form.Hidden({
        id: "rm_time_starttime",
        value: '<?php echo $rental_start_time;?>'
    });

    startTimeSelector.render('RM_start_time_selection');

    var endTimeSelector = new Ext.form.Hidden({
        id: "rm_time_endtime",
        value: '<?php echo $rental_end_time;?>'
    });

    endTimeSelector.render('RM_end_time_selection');

    <?php } ?>

    // if it's a group show the qty feild
    if ("<?php if (isset($_GET['isGroup'])){ 
            echo $_GET['isGroup'];
        } else {
            echo "0";
        }?>" === "1"){
        if (parseInt("<?php echo $qtyAvailable;?>",10) >1){
            document.getElementById("RM_quantity_selection").style.display = "block";
        }
    }

});
</script>
        </body>
</html>